﻿
namespace NetROS.Tiers.Windows.Forms
{
	/// <summary>
	/// abstract Zatvaranje typed datagridview
	/// </summary>
	[System.ComponentModel.DesignerCategoryAttribute("code")]
	public abstract class ZatvaranjeDataGridViewBase : System.Windows.Forms.UserControl
	{	
		/// <summary> 
		/// Thrown when the current Entity changed.
		/// </summary>
		public event System.EventHandler<ZatvaranjeDataGridViewEventArgs> CurrentEntityChanged;
		
		#region Fields
				
		/// <summary> 
		/// the current entity.
		/// </summary>
		private Entities.Zatvaranje _currentZatvaranje = null;
		//private Dictionary<string, string> _columnsHeaders = new Dictionary<string, string>();
		
		/// <summary> 
		/// the datagridview instance
		/// </summary>
		protected System.Windows.Forms.DataGridView uxZatvaranjeDataGridView;
		
		/// <summary> 
		/// the errorprovider
		/// </summary>
		protected System.Windows.Forms.ErrorProvider uxZatvaranjeErrorProvider;
		
		/// <summary> 
		/// the bindingsource
		/// </summary>
		protected System.Windows.Forms.BindingSource uxZatvaranjeBindingSource;
		
		
		/// <summary> 
		/// the DGV column associated with the ID property
		/// </summary>
		protected System.Windows.Forms.DataGridViewTextBoxColumn uxIDDataGridViewColumn;
		/// <summary> 
		/// the DGV column associated with the IDKnjizenje property
		/// </summary>
		protected System.Windows.Forms.DataGridViewComboBoxColumn uxIDKnjizenjeDataGridViewColumn;
		/// <summary> 
		/// the DGV column associated with the IDZatvaratelj property
		/// </summary>
		protected System.Windows.Forms.DataGridViewComboBoxColumn uxIDZatvarateljDataGridViewColumn;
		
		/// <summary> 
		/// the DGV column associated with the IznosZatvaratelja property
		/// </summary>
		protected System.Windows.Forms.DataGridViewTextBoxColumn uxIznosZatvarateljaDataGridViewColumn;
		
		#endregion
					
		#region Combo list
		
				
		private Entities.TList<Entities.KnjizenjeStavka> _IDKnjizenjeList;
		
		/// <summary> 
		/// The list of selectable KnjizenjeStavka
		/// </summary>
		[System.ComponentModel.Category("DropDownList BindingSources")]
		public Entities.TList<Entities.KnjizenjeStavka> IDKnjizenjeList
		{
			get {return this._IDKnjizenjeList;}
			set 
			{
				this._IDKnjizenjeList = value;
				this.uxIDKnjizenjeDataGridViewColumn.DataSource = null;
				this.uxIDKnjizenjeDataGridViewColumn.DataSource = this._IDKnjizenjeList;
			}
		}
		
		private bool _allowNewItemInIDKnjizenjeList;
		
		/// <summary> 
		/// Indicates if user can add an item in the list of KnjizenjeStavka
		/// </summary>
		[System.ComponentModel.Category(".netTiers"), System.ComponentModel.Description("Indicates if end user can add a new item in the combobox. an event will be thrown then.")]
		public bool AllowNewItemInIDKnjizenjeList
		{
			get { return _allowNewItemInIDKnjizenjeList;}
			set
			{
				this._allowNewItemInIDKnjizenjeList = value;
				this.uxIDKnjizenjeDataGridViewColumn.DisplayStyle = value ? System.Windows.Forms.DataGridViewComboBoxDisplayStyle.ComboBox : System.Windows.Forms.DataGridViewComboBoxDisplayStyle.DropDownButton;
			}
		}
		
				
		private Entities.TList<Entities.KnjizenjeStavka> _IDZatvarateljList;
		
		/// <summary> 
		/// The list of selectable KnjizenjeStavka
		/// </summary>
		[System.ComponentModel.Category("DropDownList BindingSources")]
		public Entities.TList<Entities.KnjizenjeStavka> IDZatvarateljList
		{
			get {return this._IDZatvarateljList;}
			set 
			{
				this._IDZatvarateljList = value;
				this.uxIDZatvarateljDataGridViewColumn.DataSource = null;
				this.uxIDZatvarateljDataGridViewColumn.DataSource = this._IDZatvarateljList;
			}
		}
		
		private bool _allowNewItemInIDZatvarateljList;
		
		/// <summary> 
		/// Indicates if user can add an item in the list of KnjizenjeStavka
		/// </summary>
		[System.ComponentModel.Category(".netTiers"), System.ComponentModel.Description("Indicates if end user can add a new item in the combobox. an event will be thrown then.")]
		public bool AllowNewItemInIDZatvarateljList
		{
			get { return _allowNewItemInIDZatvarateljList;}
			set
			{
				this._allowNewItemInIDZatvarateljList = value;
				this.uxIDZatvarateljDataGridViewColumn.DisplayStyle = value ? System.Windows.Forms.DataGridViewComboBoxDisplayStyle.ComboBox : System.Windows.Forms.DataGridViewComboBoxDisplayStyle.DropDownButton;
			}
		}
		
		#endregion
		
		#region Main Datasource
		
		private Entities.TList<Entities.Zatvaranje> _ZatvaranjeList;
				
		/// <summary> 
		/// The list of Zatvaranje to be displayed in the DGV.
		/// </summary>
		[System.ComponentModel.Category("Data")]
		public Entities.TList<Entities.Zatvaranje> ZatvaranjeList
		{
			get {return this._ZatvaranjeList;}
			set
			{
				this._ZatvaranjeList = value;
				this.uxZatvaranjeBindingSource.DataSource = null;
				this.uxZatvaranjeBindingSource.DataSource = value;
				this.uxZatvaranjeDataGridView.DataSource = null;
				this.uxZatvaranjeDataGridView.DataSource = this.uxZatvaranjeBindingSource;				
				//this.uxZatvaranjeBindingSource.ListChanged += new System.ComponentModel.ListChangedEventHandler(uxZatvaranjeBindingSource_ListChanged);
				this.uxZatvaranjeBindingSource.CurrentItemChanged += new System.EventHandler(OnZatvaranjeBindingSourceCurrentItemChanged);
			}
		}
		
		private void OnZatvaranjeBindingSourceCurrentItemChanged(object sender, System.EventArgs e)
		{
			_currentZatvaranje = uxZatvaranjeBindingSource.Current as Entities.Zatvaranje;
			
			if (_currentZatvaranje != null)
			{
				_currentZatvaranje.Validate();
			}
			//_Zatvaranje.Validate();
			OnCurrentEntityChanged();
		}

		//void uxZatvaranjeBindingSource_ListChanged(object sender, System.ComponentModel.ListChangedEventArgs e)
		//{
			//throw new Exception("The method or operation is not implemented.");
		//}		
		
		#endregion
		
		/// <summary>
		/// Gets the currently selected <c cref="Entities.Zatvaranje"/> instance.
		/// </summary>
		public Entities.Zatvaranje SelectedZatvaranje
		{
			get {return this._currentZatvaranje;}
		}
		
		#region other properties
		
		/// <summary>
		/// Gets or sets a value indicating whether you have provided your own data-management operations for the DataGridView control.
		/// </summary>
		[System.ComponentModel.Category(".netTiers")]
		public bool VirtualMode
		{
			get {return this.uxZatvaranjeDataGridView.VirtualMode;}
			set
			{
				this.uxZatvaranjeDataGridView.VirtualMode = value;
			}
		}
		
		/// <summary>
		/// Gets or sets a value indicating whether the option to add rows is displayed to the user.
		/// </summary>
		[System.ComponentModel.Category(".netTiers")]
		public bool AllowUserToAddRows
		{
			get {return this.uxZatvaranjeDataGridView.AllowUserToAddRows;}
			set {this.uxZatvaranjeDataGridView.AllowUserToAddRows = value;}
		}
		
		/// <summary>
		/// Gets or sets a value indicating whether the user is allowed to delete rows from the DataGridView.
		/// </summary>
		[System.ComponentModel.Category(".netTiers")]
		public bool AllowUserToDeleteRows
		{
			get {return this.uxZatvaranjeDataGridView.AllowUserToDeleteRows;}
			set {this.uxZatvaranjeDataGridView.AllowUserToDeleteRows = value;}
		}
		
		/// <summary>
		/// Gets the <c cref="DataGridView"/> instance.
		/// </summary>
		[System.ComponentModel.Category(".netTiers")]
		public System.Windows.Forms.DataGridView DataGridView
		{
			get {return this.uxZatvaranjeDataGridView;}
		}
		
		/// <summary>
		/// Gets a collection that contains all the columns in the control.
		/// </summary>
		[System.ComponentModel.Category(".netTiers")]		
		[System.ComponentModel.DesignerSerializationVisibilityAttribute(System.ComponentModel.DesignerSerializationVisibility.Visible)]
		//[System.ComponentModel.EditorAttribute("System.Windows.Forms.Design.DataGridViewColumnCollectionEditor, System.Design", typeof(System.Drawing.Design.UITypeEditor))] 
		public System.Windows.Forms.DataGridViewColumnCollection Columns
		{
			get { return this.uxZatvaranjeDataGridView.Columns; }
		}
		
		#endregion
		
		/// <summary> 
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.IContainer components = null;
	
		/// <summary> 
		/// Clean up any resources being used.
		/// </summary>
		/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
		protected override void Dispose(bool disposing)
		{
			if (disposing && (components != null))
			{
				components.Dispose();
			}
			base.Dispose(disposing);
		}
	
		#region Constructor
	
		/// <summary>
		/// Initializes a new instance of the <see cref="ZatvaranjeDataGridViewBase"/> class.
		/// </summary>
		public ZatvaranjeDataGridViewBase()
		{			
			this.components = new System.ComponentModel.Container();			
			
			this.uxZatvaranjeDataGridView = new System.Windows.Forms.DataGridView();
			this.uxZatvaranjeBindingSource = new System.Windows.Forms.BindingSource(this.components);
			this.uxZatvaranjeErrorProvider = new System.Windows.Forms.ErrorProvider(this.components);
			this.uxIDDataGridViewColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
			this.uxIDKnjizenjeDataGridViewColumn = new System.Windows.Forms.DataGridViewComboBoxColumn();
			this.uxIDZatvarateljDataGridViewColumn = new System.Windows.Forms.DataGridViewComboBoxColumn();
			this.uxIznosZatvarateljaDataGridViewColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
			//this.uxIDKnjizenjeBindingSource = new KnjizenjeStavkaBindingSource(this.components);
			//((System.ComponentModel.ISupportInitialize)(this.uxIDKnjizenjeBindingSource)).BeginInit();
			//this.uxIDZatvarateljBindingSource = new KnjizenjeStavkaBindingSource(this.components);
			//((System.ComponentModel.ISupportInitialize)(this.uxIDZatvarateljBindingSource)).BeginInit();
			((System.ComponentModel.ISupportInitialize)(this.uxZatvaranjeDataGridView)).BeginInit();
			((System.ComponentModel.ISupportInitialize)(this.uxZatvaranjeBindingSource)).BeginInit();
			((System.ComponentModel.ISupportInitialize)(this.uxZatvaranjeErrorProvider)).BeginInit();
			this.SuspendLayout();
						
			// 
			// uxZatvaranjeErrorProvider
			// 
			this.uxZatvaranjeErrorProvider.ContainerControl = this;
			this.uxZatvaranjeErrorProvider.DataSource = this.uxZatvaranjeBindingSource;						
			// 
			// uxZatvaranjeDataGridView
			// 
			this.uxZatvaranjeDataGridView.AutoGenerateColumns = false;
			this.uxZatvaranjeDataGridView.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
			this.uxZatvaranjeDataGridView.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
		this.uxIDDataGridViewColumn,
		this.uxIDKnjizenjeDataGridViewColumn,
		this.uxIDZatvarateljDataGridViewColumn,
		this.uxIznosZatvarateljaDataGridViewColumn			});
			this.uxZatvaranjeDataGridView.Dock = System.Windows.Forms.DockStyle.Fill;
			this.uxZatvaranjeDataGridView.Location = new System.Drawing.Point(0, 0);
			this.uxZatvaranjeDataGridView.Name = "uxZatvaranjeDataGridView";
			this.uxZatvaranjeDataGridView.Size = new System.Drawing.Size(470, 300);
			this.uxZatvaranjeDataGridView.TabIndex = 0;	
			this.uxZatvaranjeDataGridView.BackgroundColor = System.Drawing.Color.WhiteSmoke;
			this.uxZatvaranjeDataGridView.EnableHeadersVisualStyles = false;
			this.uxZatvaranjeDataGridView.DataError += new System.Windows.Forms.DataGridViewDataErrorEventHandler(this.OnZatvaranjeDataGridViewDataError);
			this.uxZatvaranjeDataGridView.CellValueNeeded += new System.Windows.Forms.DataGridViewCellValueEventHandler(this.OnZatvaranjeDataGridViewCellValueNeeded);
			this.uxZatvaranjeDataGridView.CellValuePushed += new System.Windows.Forms.DataGridViewCellValueEventHandler(this.OnZatvaranjeDataGridViewCellValuePushed);
			
			//
			// uxIDDataGridViewColumn
			//
			this.uxIDDataGridViewColumn.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.DisplayedCells;
			this.uxIDDataGridViewColumn.DataPropertyName = "ID";
			this.uxIDDataGridViewColumn.HeaderText = "ID";
			this.uxIDDataGridViewColumn.Name = "uxIDDataGridViewColumn";
			this.uxIDDataGridViewColumn.Resizable = System.Windows.Forms.DataGridViewTriState.True;
			this.uxIDDataGridViewColumn.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic;
			this.uxIDDataGridViewColumn.ReadOnly = true;		
			//
			// uxIDKnjizenjeDataGridViewColumn
			//
			this.uxIDKnjizenjeDataGridViewColumn.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.DisplayedCells;
			this.uxIDKnjizenjeDataGridViewColumn.DataPropertyName = "IDKnjizenje";
			this.uxIDKnjizenjeDataGridViewColumn.HeaderText = "IDKnjizenje";
			this.uxIDKnjizenjeDataGridViewColumn.Name = "uxIDKnjizenjeDataGridViewColumn";
			this.uxIDKnjizenjeDataGridViewColumn.Resizable = System.Windows.Forms.DataGridViewTriState.True;
			this.uxIDKnjizenjeDataGridViewColumn.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic;
			this.uxIDKnjizenjeDataGridViewColumn.ReadOnly = false;		
			//
			// uxIDZatvarateljDataGridViewColumn
			//
			this.uxIDZatvarateljDataGridViewColumn.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.DisplayedCells;
			this.uxIDZatvarateljDataGridViewColumn.DataPropertyName = "IDZatvaratelj";
			this.uxIDZatvarateljDataGridViewColumn.HeaderText = "IDZatvaratelj";
			this.uxIDZatvarateljDataGridViewColumn.Name = "uxIDZatvarateljDataGridViewColumn";
			this.uxIDZatvarateljDataGridViewColumn.Resizable = System.Windows.Forms.DataGridViewTriState.True;
			this.uxIDZatvarateljDataGridViewColumn.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic;
			this.uxIDZatvarateljDataGridViewColumn.ReadOnly = false;		
			//
			// uxIznosZatvarateljaDataGridViewColumn
			//
			this.uxIznosZatvarateljaDataGridViewColumn.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.DisplayedCells;
			this.uxIznosZatvarateljaDataGridViewColumn.DataPropertyName = "IznosZatvaratelja";
			this.uxIznosZatvarateljaDataGridViewColumn.HeaderText = "IznosZatvaratelja";
			this.uxIznosZatvarateljaDataGridViewColumn.Name = "uxIznosZatvarateljaDataGridViewColumn";
			this.uxIznosZatvarateljaDataGridViewColumn.Resizable = System.Windows.Forms.DataGridViewTriState.True;
			this.uxIznosZatvarateljaDataGridViewColumn.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic;
			this.uxIznosZatvarateljaDataGridViewColumn.ReadOnly = false;		
			//
			// uxIDKnjizenjeDataGridViewColumn
			//				
			this.uxIDKnjizenjeDataGridViewColumn.DisplayMember = "IDKonto";	
			this.uxIDKnjizenjeDataGridViewColumn.ValueMember = "ID";	
			this.uxIDKnjizenjeDataGridViewColumn.DisplayStyleForCurrentCellOnly = true;
			//uxIDKnjizenjeDataGridViewColumn.DataSource = uxIDKnjizenjeBindingSource;				
				
			//
			// uxIDZatvarateljDataGridViewColumn
			//				
			this.uxIDZatvarateljDataGridViewColumn.DisplayMember = "IDKonto";	
			this.uxIDZatvarateljDataGridViewColumn.ValueMember = "ID";	
			this.uxIDZatvarateljDataGridViewColumn.DisplayStyleForCurrentCellOnly = true;
			//uxIDZatvarateljDataGridViewColumn.DataSource = uxIDZatvarateljBindingSource;				
				
			
			// this
			this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
			this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
			this.Controls.Add(this.uxZatvaranjeDataGridView);
			this.Name = "ZatvaranjeDataGridView";
			this.Size = new System.Drawing.Size(470, 300);
			//((System.ComponentModel.ISupportInitialize)(this.uxIDKnjizenjeBindingSource)).EndInit();
			//((System.ComponentModel.ISupportInitialize)(this.uxIDZatvarateljBindingSource)).EndInit();
			((System.ComponentModel.ISupportInitialize)(this.uxZatvaranjeErrorProvider)).EndInit();
			((System.ComponentModel.ISupportInitialize)(this.uxZatvaranjeDataGridView)).EndInit();
			((System.ComponentModel.ISupportInitialize)(this.uxZatvaranjeBindingSource)).EndInit();
			this.ResumeLayout(false);
		}
		#endregion
				
		#region events
		
		/// <summary>
		/// Raised the CurrentEntityChanged event.
		/// </summary>
		protected void OnCurrentEntityChanged()
		{
			if (CurrentEntityChanged != null)
			{
				ZatvaranjeDataGridViewEventArgs args = new ZatvaranjeDataGridViewEventArgs();
				args.Zatvaranje = _currentZatvaranje;
				CurrentEntityChanged(this, args); 
			}
		}
		
		#endregion
		
		/// <summary>
		/// Strongly typed event 
		/// </summary>
		public class ZatvaranjeDataGridViewEventArgs : System.EventArgs
		{
			private Entities.Zatvaranje	_Zatvaranje;
	
			/// <summary>
			/// the  Entities.Zatvaranje instance.
			/// </summary>
			public Entities.Zatvaranje Zatvaranje
			{
				get { return _Zatvaranje; }
				set { _Zatvaranje = value; }
			}	
		}
		
		/// <summary>
		/// Handles the DataError event of the uxZatvaranjeDataGridView control.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">The <see cref="T:System.Windows.Forms.DataGridViewDataErrorEventArgs"/> instance containing the event data.</param>
		protected void OnZatvaranjeDataGridViewDataError(object sender, System.Windows.Forms.DataGridViewDataErrorEventArgs e)
		{
			System.Diagnostics.Debug.WriteLine(string.Format( "{0}, {1}", e.ColumnIndex, e.RowIndex));
		}
		
		/// <summary>
		/// Handles the CellValueNeeded event of the uxZatvaranjeDataGridView control.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">The <see cref="T:System.Windows.Forms.DataGridViewCellValueEventArgs"/> instance containing the event data.</param>
		protected void OnZatvaranjeDataGridViewCellValueNeeded(object sender, System.Windows.Forms.DataGridViewCellValueEventArgs e)
		{
			switch(uxZatvaranjeDataGridView.Columns[e.ColumnIndex].Name)
			{
					case "uxIDDataGridViewColumn":
						e.Value = ZatvaranjeList[e.RowIndex].ID;
						break;
					case "uxIDKnjizenjeDataGridViewColumn":
						e.Value = ZatvaranjeList[e.RowIndex].IDKnjizenje;
						break;
					case "uxIDZatvarateljDataGridViewColumn":
						e.Value = ZatvaranjeList[e.RowIndex].IDZatvaratelj;
						break;
					case "uxIznosZatvarateljaDataGridViewColumn":
						e.Value = ZatvaranjeList[e.RowIndex].IznosZatvaratelja;
						break;
				default:
				break;
			}
		}
		
		/// <summary>
		/// Handles the CellValuePushed event of the uxZatvaranjeDataGridView control.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">The <see cref="T:System.Windows.Forms.DataGridViewCellValueEventArgs"/> instance containing the event data.</param>
		protected void OnZatvaranjeDataGridViewCellValuePushed(object sender, System.Windows.Forms.DataGridViewCellValueEventArgs e)
		{
			switch(uxZatvaranjeDataGridView.Columns[e.ColumnIndex].Name)
			{
					case "uxIDDataGridViewColumn":
						ZatvaranjeList[e.RowIndex].ID = (System.Int32)e.Value;
						break;
					case "uxIDKnjizenjeDataGridViewColumn":
						ZatvaranjeList[e.RowIndex].IDKnjizenje = (System.Int32)e.Value;
						break;
					case "uxIDZatvarateljDataGridViewColumn":
						ZatvaranjeList[e.RowIndex].IDZatvaratelj = (System.Int32)e.Value;
						break;
					case "uxIznosZatvarateljaDataGridViewColumn":
						ZatvaranjeList[e.RowIndex].IznosZatvaratelja = (System.Decimal)e.Value;
						break;
				default:
				break;
			}
		}
	}
}
